#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int N = 110, M = 2 * N;
int f[N][N];
int n, m;
int h[N], e[M], ne[M], idx, w[M];
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int fa)
{
	for (int i = h[u]; ~i; i = ne[i]) {
		int z = e[i];
		if (z == fa)continue;
		dfs(z, u);
		for (int j = m; j >= 0; j--) {
			for (int k = 0; k < j; k++) {
				f[u][j] = max(f[u][j], f[u][j - k - 1] + f[z][k] + w[i]);
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 0; i < n - 1; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	dfs(1, 0);
	cout << f[1][m] << endl;
	return 0;
}
